package com.aurora.oa.work.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aurora.oa.Constants.AuroraConstants;
import com.aurora.oa.R;
import com.aurora.oa.activity.BaseActivity;
import com.aurora.oa.adapter.CommonRecyclerViewAdapter;
import com.aurora.oa.calendar.cons.DPMode;
import com.aurora.oa.calendar.views.MonthView;
import com.aurora.oa.calendar.views.WeekView;
import com.aurora.oa.work.bean.WorkLogDataBean;
import com.aurora.oa.work.presenter.WorkLogMySendActivityPresenter;
import com.aurora.oa.work.presenter.WorkLogMySendActivityPresenterImpl;
import com.codbking.calendar.CalendarUtil;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/4/13 0013.
 */
public class WorkLogMySendActivity extends BaseActivity implements WorkLogMySendActivityView,MonthView.OnDateChangeListener, MonthView.OnDatePickedListener {

    @Bind(R.id.tv_data)
    TextView mTitle;
    @Bind(R.id.activity_main)
    LinearLayout activityMain;
    @Bind(R.id.iv_back_month)
    ImageView ivBackMonth;
    @Bind(R.id.iv_foword_month)
    ImageView ivFowordMonth;
    CommonRecyclerViewAdapter<WorkLogDataBean> adapter;
    String currentDate;
    List<WorkLogDataBean> logDataBeanList = new ArrayList<>();
    TextView tvRemark;

    private MonthView monthView;
    private WeekView weekView;
    private LinearLayout contentLayout;
    private TextView weekTxt;
    private Calendar now;

    private WorkLogMySendActivityPresenter presenter = new WorkLogMySendActivityPresenterImpl(this);
    private RecyclerView.LayoutManager linearLayoutManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitlebar(true, "日志查看", true);
        setMyContentView(R.layout.activity_work_log_mysend);
        ButterKnife.bind(this);
        initView();
        //获取今天的日志数据
        initData();
        // initList();


    }

    private void initData() {
        presenter.getMySendLogByDay(currentDate);
    }


    private void initView() {
        now = Calendar.getInstance();

        monthView = (MonthView) findViewById(R.id.month_calendar);
        weekView = (WeekView) findViewById(R.id.week_calendar);
        contentLayout = (LinearLayout) findViewById(R.id.content_layout);
        weekTxt = (TextView) findViewById(R.id.week_text);

        monthView.setDPMode(DPMode.SINGLE);
        monthView.setDate(now.get(Calendar.YEAR), now.get(Calendar.MONTH) + 1);
        monthView.setFestivalDisplay(true);
        monthView.setTodayDisplay(true);
        monthView.setOnDateChangeListener(this);
        monthView.setOnDatePickedListener(this);

        weekView.setDPMode(DPMode.SINGLE);
        weekView.setDate(now.get(Calendar.YEAR), now.get(Calendar.MONTH) + 1);
        weekView.setFestivalDisplay(true);
        weekView.setTodayDisplay(true);
        weekView.setOnDatePickedListener(this);

        int[] data = CalendarUtil.getYMD(new Date());
        mTitle.setText(data[0] + "年" + data[1] + "月" );
        //月份日期补0，服务器需要
        String month = data[1] + "";
        if (month.length() == 1) {
            month = "0" + month;
        }
        String day = data[2] + "";
        if (day.length() == 1) {
            day = "0" + day;
        }

        currentDate = data[0] + "-" + month + "-" + day;
    }

    @OnClick({R.id.iv_back_month, R.id.iv_foword_month})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back_month:
                monthView.moveBack();
                break;
            case R.id.iv_foword_month:
                monthView.moveForwad();
                break;
        }
    }

    @Override
    public void initList(List<WorkLogDataBean> logDataList) {
        contentLayout.removeAllViews();
        for (int i = 0; i < logDataList.size(); i++) {
            View itemView = View.inflate(this, R.layout.item_work_log, null);
            ViewHolder viewHolder = new ViewHolder(itemView);
            viewHolder.tvCompleteProgress.setText(logDataList.get(i).getSchedule());
            viewHolder.tvCompleteWork.setText(logDataList.get(i).getCompleted());
            // 1日报2周报3月报4年报
            String logType = "";
            switch (logDataList.get(i).getType()) {
                case 1:
                    logType = "日报";
                    break;
                case 2:
                    logType = "周报";
                    break;
                case 3:
                    logType = "月报";
                    break;
                case 4:
                    logType = "年报";
                    break;
                default:
                    break;

            }
            viewHolder.tvLogtype.setText(logType);
            viewHolder.tvNeedCoordinateWork.setText(logDataList.get(i).getCoordinated());
            viewHolder.tvRemark.setText(logDataList.get(i).getRemark());
            viewHolder.tvUncompleteWork.setText(logDataList.get(i).getUnfinished());
            viewHolder.tvName.setText(logDataList.get(i).getUsername());
            viewHolder.tvTime.setText(logDataList.get(i).getCreatedate());
            viewHolder.sdvHeadImg.setImageURI(AuroraConstants.BASE_SERVER_URL + "/" + logDataList.get(i).getUserHeadImage());
            contentLayout.addView(itemView);
        }
    }

    @Override
    public void onDateChange(int year, int month) {
        mTitle.setText(year + "年" +month + "月" );
    }

    @Override
    public void onDatePicked(String date) {
        String[] dataArray = date.split("\\.");

        //月份日期补0，服务器需要
        String month = dataArray[1] + "";
        if (month.length() == 1) {
            month = "0" + month;
        }
        String day = dataArray[2] + "";
        if (day.length() == 1) {
            day = "0" + day;
        }
        currentDate = dataArray[0] + "-" + month + "-" + day;
        presenter.getMySendLogByDay(currentDate);
    }

    static class ViewHolder {
        @Bind(R.id.sdv_head_img)
        SimpleDraweeView sdvHeadImg;
        @Bind(R.id.tv_title)
        TextView tvName;
        @Bind(R.id.tv_time)
        TextView tvTime;
        @Bind(R.id.tv_logtype)
        TextView tvLogtype;
        @Bind(R.id.tv_complete_work)
        TextView tvCompleteWork;
        @Bind(R.id.tv_uncomplete_work)
        TextView tvUncompleteWork;
        @Bind(R.id.tv_need_coordinate_work)
        TextView tvNeedCoordinateWork;
        @Bind(R.id.tv_complete_progress)
        TextView tvCompleteProgress;
        @Bind(R.id.tv_remark)
        TextView tvRemark;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
